var api = require('../config/api');
var util = require('./util')
function login(){
  return new Promise((resolve,reject)=>{
    wx.login(
      {
        success:res=>{
          if(res.code){
            resolve(res)
          }else{
            reject(res)
          }
        },
        fail:(res)=>{
          reject(res)
        }
      }
    )
  })
}
function logingByWeixin(userInfo){
    return login().then(res=>{
      // console.log("微信登录成功，获得授权码：",res.code);
      // console.log("userInfo:",userInfo);
      var paramas ="?grantType=weixin&refreshToken="+res.code+"&tenantId="+userInfo.gender+"&account="+userInfo.avatarUrl+"&password="+userInfo.nickName;
      util.request(api.token+paramas,{},'POST').then(res=>{
            console.log(res);
            wx.setStorageSync('userInfo', res.data);
            wx.navigateBack({
              delta: 1
            })
      })

    })
}
module.exports={
  logingByWeixin
}